package com.tiandang.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import tiandang.core.error.BusinessException;
import tiandang.core.model.Manager;
import tiandang.core.response.CommonReturnType;
import tiandang.core.service.ManagerService;
import tiandang.core.service.RewardLogService;

import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/api/manager")
public class ManagerController {

  @Autowired
  private ManagerService managerService;

  @Autowired
  private RewardLogService rewardLogService;

  @GetMapping("/getList")
  public CommonReturnType getList(@RequestParam(value = "name", required = false) String name,
                                  @RequestParam(value = "currentPage", defaultValue = "1") Integer currentPage,
                                  @RequestParam(value = "limit", defaultValue = "10") Integer limit) {

    return CommonReturnType.success(managerService.getList(name, currentPage, limit));
  }

  @PostMapping("/add")
  public CommonReturnType add(Manager manager) throws BusinessException {
    managerService.save(manager);
    return CommonReturnType.success();
  }

  @PostMapping("/update")
  public Object update(Manager manager) throws BusinessException {
    managerService.update(manager);
    return CommonReturnType.success();
  }


  @PostMapping("/delete")
  public Object delete(Integer id) {
    managerService.delete(id);
    return CommonReturnType.success();
  }

  @GetMapping("/getDetail")
  public Object getDetail(Integer id) {
    Map<String, Object> resultMap = new HashMap<>();
    resultMap.put("manager", managerService.getManagerById(id));
    return CommonReturnType.success(resultMap);
  }

  // 冻结/解冻
  @PostMapping("/setFrozen")
  public CommonReturnType setFrozen(@RequestParam(value = "id") Integer id, @RequestParam(value = "status") int status) {
    managerService.setFrozen(id, status);
    return CommonReturnType.success();
  }

  //结算佣金
  @PostMapping("/balanceReward")
  public CommonReturnType balanceReward(@RequestParam(value = "id") Integer id) {
    managerService.balanceReward(id);
    return CommonReturnType.success();
  }

  @GetMapping("/getRewardLogList")
  public CommonReturnType getRewardLogList(@RequestParam(value = "managerId") Integer managerId,
                                           @RequestParam(value = "currentPage", defaultValue = "1") Integer currentPage,
                                           @RequestParam(value = "limit", defaultValue = "10") Integer limit) {

    return CommonReturnType.success(rewardLogService.getList(managerId, currentPage, limit));
  }

  @GetMapping("/getAllNormal")
  public CommonReturnType getAllNormal() {
    Map<String, Object> resultMap = new HashMap<>();
    resultMap.put("managerList", managerService.getAllNormal());
    return CommonReturnType.success(resultMap);
  }
}
